package com.compass.mobile.ui.viewModel

import androidx.lifecycle.MutableLiveData
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.DepartmentBean
import com.compass.mobile.models.Doctor
import com.compass.mobile.models.Inspection
import com.compass.mobile.models.OrderBean
import com.compass.mobile.models.OrderBeanWarp
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel

/**
 * Description：问诊
 * Date:2024/6/4 14:00
 */
class InquiryViewModel : BaseViewModel() {
    val departmentListLiveData = MutableLiveData<Map<String, MutableList<DepartmentBean>?>?>()
    val departmentLiveData = MutableLiveData<DepartmentBean?>()
    val consultationOrderLiveData = MutableLiveData<Any?>()
    val doctorsLiveData = MutableLiveData<MutableList<Doctor>?>()
    val ordersLiveData = MutableLiveData<MutableList<OrderBean>?>()
    val inspectionLiveData = MutableLiveData<MutableList<Inspection>?>()
    val inspectionDetailsLiveData = MutableLiveData<Inspection?>()
    val orderDetailsLiveData = MutableLiveData<OrderBean?>()


    fun geDeptList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getLetterDeptList(null)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            departmentListLiveData.value = it
        })
    }

    fun gePatientOrderDetails(id:String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.gePatientOrderDetails(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            orderDetailsLiveData.value = it
        })
    }

    fun geInspectionOrder(orderId:String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geInspectionOrder(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            inspectionDetailsLiveData.value = it
        })
    }

    fun geDeptListTwo(pageNo: Int=1, pageSize: Int = 1000) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptListTwo(pageNo,pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            departmentListLiveData.value = it
        })
    }



    fun consultationOrder(orderId:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.consultationOrder(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {

            consultationOrderLiveData.value=it
        })
    }




    fun geDeptDetails(deptId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptDetails(deptId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            departmentLiveData.value = it
        })
    }


    /**
     * @param type  1是查医生
     * 获取问诊医生
     */
    fun getInquiryDoctors(deptId: String?,type:Int=1, pageNo: Int, pageSize: Int = 4) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDoctors(deptId, type,pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                doctorsLiveData.value = null
            } else {
                doctorsLiveData.value = it.list
            }
        })
    }

    /**
     * @param codeDept 科室编码code
     * @param onlyHaveSch 是否全查，0表示全查，1表示只查有排班的
     * @param onlyExpert  是否只查专家 ，0表示全查，1表示只查专家的
     */
    fun geRecommendDoctors(codeDept: String?,onlyHaveSch:Int=1, onlyExpert:Int=0,pageNo: Int, pageSize: Int = 4) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geRecommendDoctors(codeDept, onlyHaveSch,onlyExpert,pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                doctorsLiveData.value = null
            } else {
                doctorsLiveData.value = it.list
            }
        })
    }


    //获取问诊列表
    fun getConsultationList(patientId: String?, type: String?, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationList(patientId, type, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                ordersLiveData.value=null
            } else {
                ordersLiveData.value = it.list
            }
        })
    }

    //获取检验检查列表
    fun getInspectionList(patientId: String?, type: String?, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInspectionList(patientId, type, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                inspectionLiveData.value=null
            } else {
                inspectionLiveData.value = it.list
            }
        })
    }


}
